<template>
    <el-dialog
      title="编辑物流服务商"
      :visible.sync="dialogVisible"
      width="1050px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="closeDialog"
      custom-class="custom-class-line">
      <div>
        <el-radio-group v-model="activeName" class="mb20">
          <el-radio-button label="basic">基本资料</el-radio-button>
          <el-radio-button label="address">运输线路</el-radio-button>
        </el-radio-group>
        <BasicEdit v-show="activeName === 'basic'" :areaList="allInfoOptions" :userInfo="userInfo" :goodstype="dict.type.goods_type" :dept="deptInfo" ref="basic"></BasicEdit>
        <Address v-show="activeName === 'address'" @area="area" :areaList="areaList"  :data="deptInfo"  ref="address"
        ></Address>
        <div class="text-center">
          <el-button type="primary" style="width:100px" @click="confirmAdd">确定</el-button>
          <el-button style="width:100px" @click="closeDialog">取消</el-button>
        </div>
      </div>
    </el-dialog>
</template>

<script>
import BasicEdit from './basic/basicEdit.vue'
import Address from './address/addresss.vue'
import { arrSubtraction } from "@/utils/zito"
import { getByDeptIdDetail,updateUserBasic,updateBossEditDept  } from "@/api/login";
export default {
  dicts: ['goods_type'],
  name: 'edit',
  components: {
    BasicEdit,
    Address
  },
  props: {
    areaList: {
      type: Array,
      default: []
    },
    allInfoOptions: {
      type: Array,
      default: []
    },
  },
  data() {
    return {
      dialogVisible: false,
      activeName: 'basic',
      form: {},
      userInfo: {
        dept:{
          lineList:[]
        }
      },
      refList: [
        'basic',
        'address',
      ],
      areaInfo: [],
      deptInfo:{}
    };
  },
  methods: {
    area(e) {
      this.areaInfo = e
    },
    showDialog(row) {
      this.dialogVisible = true
      this.userInfo = row
      //this.userInfo.roleIds = ['1']
      //this.userInfo.status = '0'
      this.loading = true
      this.deptInfo={}
      getByDeptIdDetail(row.deptId).then(res => {
        this.deptInfo=res.data
      }).finally(() => {
        this.loading = false
      });
    },
    closeDialog() {
      this.dialogVisible = false
      // this.activeName = 'basic'
      // this.$refs.address.onClear()
      this.$emit('close')
    },
    async confirmAdd() {
      const valid = await this.$refs[this.activeName].handleValidate()
      if (valid) {
        let promiseArray = [];
        const remainRefList = arrSubtraction(this.refList, [this.activeName])
        for(let i = 0;i<remainRefList.length;i++){
          promiseArray.push(new Promise(async(resolve) => {
            const result = await this.$refs[remainRefList[i]].handleValidate()
            if (result) resolve(true)
            else resolve(false)
          }))
        }
        const resultArray = await Promise.all(promiseArray)
        const resultAll = resultArray.every(v => v)
        if (resultAll) {
          //修改线路的字段名,与后端保持一致
          this.deptInfo=this.$refs.basic.form
          this.deptInfo.deptLineList=this.$refs.address.form.list
          //承运货物
          this.deptInfo.carryingGoods=this.$refs.basic.carryingGoods_.join(',')
          //如果当前服务商未存在信息
          if(this.userInfo.dept.deptId===null) this.deptInfo.deptId=this.userInfo.deptId
          updateBossEditDept(this.deptInfo).then(res => {
            this.$modal.msgSuccess("编辑成功");
            this.closeDialog()
          });
          // updateUserBasic(
          //   {
          //     ...this.userInfo,
          //     phonenumber: this.form.user_phonenumber,
          //     status: this.form.user_status,
          //     roleIds: this.form.user_roleIds,
          //     sessionTimeout: this.form.user_sessionTimeout,
          //     checkStatus: '1',
          //     dept: {
          //       ...this.form
          //     }
          //   }
          // ).then(res => {
          //   console.log(res)
          //   this.dialogVisible = false
          // });
        } else {
          this.$message.error('请完善其他信息')
        }
      }
    }
  }
};
</script>
